package syscommand

import (
	"errors"
	"flag"
	"strings"
	"time"
    
    "collecter/app/common/enum"
    "collecter/app/common/helper"
)

type BaseCmd struct {
}

var FlagSet *flag.FlagSet

func NewFlagSet() {
	FlagSet = flag.NewFlagSet("console", flag.ExitOnError)
}

func (c *BaseCmd) BoolVar(p *bool, name string, value bool, usage string) {
	FlagSet.BoolVar(p, name, value, usage)
}

func (c *BaseCmd) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
	FlagSet.DurationVar(p, name, value, usage)
}
func (c *BaseCmd) Float64Var(p *float64, name string, value float64, usage string) {
	FlagSet.Float64Var(p, name, value, usage)
}
func (c *BaseCmd) Int64Var(p *int64, name string, value int64, usage string) {
	FlagSet.Int64Var(p, name, value, usage)
}
func (c *BaseCmd) IntVar(p *int, name string, value int, usage string) {
	FlagSet.IntVar(p, name, value, usage)
}
func (c *BaseCmd) StringVar(p *string, name string, value string, usage string) {
	FlagSet.StringVar(p, name, value, usage)
}
func (c *BaseCmd) Uint64Var(p *uint64, name string, value uint64, usage string) {
	FlagSet.Uint64Var(p, name, value, usage)
}
func (c *BaseCmd) UintVar(p *uint, name string, value uint, usage string) {
	FlagSet.UintVar(p, name, value, usage)
}

func (c *BaseCmd) ParsePathAndPkg(name string, cmdType string) (path string, pkg string, object string, err error) {
	var cmdTypes = map[string]string{
		"controller":    "controller",
		"model":         "model",
		"middleware":    "middleware",
		"command":       "command",
		"queueJob":      "queue/jobs",
		"eventListener": "event/listener",
	}
	// var pkg,object,path string
	var cmdTypeRoot string
	if val, ok := cmdTypes[cmdType]; !ok {
		return path, pkg, object, errors.New("参数错误")
	} else {
		cmdTypeRoot = val
	}
	path = enum.APP_DIR + cmdTypeRoot + "/"
	if strings.Contains(name, "/") {
		nameInfos := strings.Split(name, "/")
		nameLen := len(nameInfos)
		if nameLen > 2 {
			for i := 0; i < nameLen; i++ {
				if i == nameLen-2 {
					pkg = nameInfos[i]
				}
				if i == nameLen-1 {
					object = nameInfos[i]
					break
				}
				path += nameInfos[i] + "/"
			}
		} else {
			pkg = nameInfos[0]
			object = nameInfos[1]
			path += pkg + "/"
		}
	} else {
		pkg = cmdType
		object = name
	}
	// fmt.Println("======******======", mc.CtlName, pkg, controller, path)
	if err := helper.MkDir(path); err != nil {
		return path, pkg, object, err
	}
	return path, pkg, object, nil
}
